package com.example.smartcitymovie.common;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.RecycleViewAdapter;
import com.example.smartcitymovie.bean.TheaterTimesListBean;
import com.example.smartcitymovie.dataRepo.ApiData;
import com.example.smartcitymovie.ui.movie.ChooseTharterTimes;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ShowFragmentList#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ShowFragmentList extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ID = "id";
    private View view;
    private RecycleViewPlus RVShowList;



    // TODO: Rename and change types of parameters
    private Integer id;


    public ShowFragmentList() {
        // Required empty public constructor
    }


    public static ShowFragmentList newInstance(int id) {
        ShowFragmentList fragment = new ShowFragmentList();
        Bundle args = new Bundle();
        args.putInt(ID, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view  = inflater.inflate(R.layout.fragment_show_list, container, false);
        RVShowList = (RecycleViewPlus) view.findViewById(R.id.RV_ShowList);
        initView();
        return view;
    }

    private void initView() {
        TheaterTimesListBean theaterTimesListBean = ApiData.getTheaterTimesListBean();
        requireActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                RVShowList.setLinearLayoutManager(LinearLayout.VERTICAL);
                RVShowList.setAdapter(theaterTimesListBean.getRows(),R.layout.theater_times_list_item_layout);
                RVShowList.getBaseAdapter().setOnRecycleViewItemCLickListener(new RecycleViewAdapter.OnRecycleViewItemCLickListener() {
                    @Override
                    public void onRecycleViewItemCLickListener(View view, int position) {
                        Intent intent = new Intent(getActivity(), ChooseTharterTimes.class);
                        intent.putExtra("id",id);
                        intent.putExtra("tharterId",theaterTimesListBean.getRows().get(position).getTheaterId());
                        startActivity(intent);
                    }
                });
            }
        });
    }
}